#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>

using namespace std;

void handler(int sig)
{
    pid_t id;
    while (true)
    {
        pid_t rid = waitpid(-1, nullptr, WNOHANG);
        if (rid > 0)
            cout << "wait child success, pid: " << rid << endl;
        else if (rid <= 0)
            break;
    }
    cout << "wait sub process done" << endl;
}

int main()
{
    //signal(SIGCHLD, handler);
    signal(SIGCHLD, SIG_IGN);
    for (int i = 0; i < 100; i++)
    {
        pid_t id = fork();
        if (id == 0)
        {
            int cnt = 5;
            while (cnt--)
            {
                cout << "I am child process, pid:" << getpid() << endl;
                sleep(1);
            }
            cout << "child process exit" << endl;
            exit(0);
        }
    }

    while (true)
        sleep(1);
}